<form id="formEdit" action="save" class="form-horizontal" role="form">
	<div class="modal-dialog  modal-lg" role="document">
		<div class="modal-content">
			<div class="modal-header">
				<button type="button" class="close" data-dismiss="modal"
					aria-label="Close">
					<span aria-hidden="true">&times;</span>
				</button>
				<h5 class="modal-title" id="myModalLabel">操作</h5>
			</div>
			<div class="modal-body">

				<!-- 以下为要修改的的内容 -->

				<input type="hidden" name="id"
					data-th-value="${model==null?null:model.id}">

				<div class="row">
					<div class="form-group col-sm-4">
						<label for="txt_college" class="col-sm-5 control-label"><span
							class="text-red">*</span>年级</label>
						<div class="col-sm-7">
							<select id="year" name="year" class="form-control"
								onchange="selectYear(this.value)">
								<option style="display: none"></option>
								<option data-th-if="${years!=null}" data-th-each="year:${years}"
									data-th-value="${year}" data-th-text="${year}">1</option>
							</select>

						</div>
					</div>
					<div class="form-group col-sm-4">
						<label for="txt_college" class="col-sm-5 control-label"><span
							class="text-red">*</span>学院</label>
						<div class="col-sm-7">
							<select class="form-control" id="college" name="college"
								onchange="selectCollege(this.value)">
								<option style="display: none"></option>
							</select>
						</div>
					</div>
					<div class="form-group col-sm-4">
						<label for="text_professions" class="col-sm-5 control-label"><span
							class="text-red">*</span>专业</label>
						<div class="col-sm-7">
							<select class="form-control" id="profesion" name="profesion">
								<option style="display: none"></option>
							</select>
						</div>
					</div>
				</div>

				<div class="row">
					<div data-th-if="${model==null}">
						<div class="form-group col-sm-4">
							<label for="text_kc" class="col-sm-5 control-label">课程</label>
							<div class="col-sm-7">
								<select class="form-control" id="kc" name="kc" onchange="setKc()">
									<option style="display: none"></option>
								</select>
							</div>
						</div>
					</div>

					<div data-th-if="${model!=null}">
						<div class="form-group col-sm-4">
							<label for="txt_kcmc" class="col-sm-5 control-label"><span
								class="text-red">*</span>课程名称</label>
							<div class="col-sm-7">
								<input type="text" name="kcmc" class="form-control"
									placeholder="课程名称" data-th-value="${model==null?'':model.kcmc}">
							</div>
						</div>
					</div>
					<div class="form-group col-sm-4">
						<label for="txt_khfs" class="col-sm-5 control-label">考核方式</label>
						<div class="col-sm-7">
							<!-- <input type="text" name="khfs" class="form-control" 
								placeholder="考核方式" data-th-value="${model==null?'':model.khfs}"> -->
							<select id="khfs" name="khfs" class="selectpicker form-control">
								<option style="display: none;"></option>
								<option value="考试">考试</option>
								<option value="考核">考核</option>
							</select>
						</div>
					</div>
					<div class="form-group col-sm-4">
						<label for="txt_kkxq" class="col-sm-5 control-label">开课学期</label>
						<div class="col-sm-7">
							<select name="kkxq" class="form-control" id="kkxq">
								<option style="display: none"></option>
								<option value="一">一</option>
								<option value="二">二</option>
								<option value="三">三</option>
								<option value="四">四</option>
								<option value="五">五</option>
								<option value="六">六</option>
							</select>
						</div>
					</div>
				</div>


				<div id="kcbContent">
					<div class="row">

						<div class="form-group col-sm-4">
							<label for="txt_kcdm" class="col-sm-5 control-label"><span
								class="text-red">*</span>课程代码</label>
							<div class="col-sm-7">
								<input type="text" name="kcdm" class="form-control"
									placeholder="课程代码" data-th-value="${model==null?'':model.kcdm}">
							</div>
						</div>
						<div class="form-group col-sm-4">
							<label for="txt_kclb" class="col-sm-5 control-label"><span
								class="text-red">*</span>课程类别</label>
							<div class="col-sm-7">
								<select id="lbSelect" class="selectpicker form-control"
									data-live-search="false" multiple="multiple" name="kclb"
									data-max-options="2" data-min-options="2">
									<option data-th-each="c:${courseCategorys}"
										data-th-value="${c.id}" data-th-text="${c.type}">类别1</option>

								</select>
							</div>
						</div>
						<div class="form-group col-sm-4">
							<label for="txt_zhouxs" class="col-sm-5 control-label">每周学时</label>
							<div class="col-sm-7">
								<input type="text" name="zhouxs" class="form-control"
									placeholder="每周学时"
									data-th-value="${model==null?'':model.zhouxs}">
							</div>
						</div>
					</div>


					<div class="row">
						<div class="form-group col-sm-4">
							<label for="txt_zongxs" class="col-sm-5 control-label">总学时</label>
							<div class="col-sm-7">
								<input type="text" name="zongxs" class="form-control"
									placeholder="总学时"
									data-th-value="${model==null?'':model.zongxs}">
							</div>
						</div>
						<div class="form-group col-sm-4">
							<label for="txt_jsxs" class="col-sm-5 control-label">讲授学时</label>
							<div class="col-sm-7">
								<input type="text" name="jsxs" class="form-control"
									placeholder="讲授学时" data-th-value="${model==null?'':model.jsxs}">
							</div>
						</div>
						<div class="form-group col-sm-4">
							<label for="txt_syxs" class="col-sm-5 control-label">实验学时</label>
							<div class="col-sm-7">
								<input type="text" name="syxs" class="form-control"
									placeholder="实验学时" data-th-value="${model==null?'':model.syxs}">
							</div>
						</div>
					</div>


					<div class="row">
						<div class="form-group col-sm-4">
							<label for="txt_sjxs" class="col-sm-5 control-label">上机学时</label>
							<div class="col-sm-7">
								<input type="text" name="sjxs" class="form-control"
									placeholder="上机学时" data-th-value="${model==null?'':model.sjxs}">
							</div>
						</div>
						<div class="form-group col-sm-4">
							<label for="txt_qtxs" class="col-sm-5 control-label">其他学时</label>
							<div class="col-sm-7">
								<input type="text" name="qtxs" class="form-control"
									placeholder="其他学时" data-th-value="${model==null?'':model.qtxs}">
							</div>
						</div>

						<div class="form-group col-sm-4">
							<label for="txt_xf" class="col-sm-5 control-label">学分</label>
							<div class="col-sm-7">
								<input type="text" name="xf" class="form-control" id="xf"
									placeholder="学分" data-th-value="${model==null?'':model.xf}">
							</div>
						</div>
					</div>
				</div>
				<!--/kcbContent  -->

				<!-- 以上为要修改的内容 -->

			</div>
			<div class="modal-footer">
				<button type="submit" class="btn btn-primary">
					<span class="glyphicon glyphicon-floppy-disk" aria-hidden="true"></span>保存
				</button>
				<button type="button" class="btn btn-default" data-dismiss="modal">
					<span class="glyphicon glyphicon-remove" aria-hidden="true"></span>关闭
				</button>
			</div>
		</div>
	</div>
</form>

<script type="text/javascript" th:inline="javascript">
	$("#lbSelect").selectpicker('refresh');
	$("#year").selectpicker({
		title : '选择年级'
	});
	$("#college").selectpicker({
		title : '选择学院'
	});
	$("#profesion").selectpicker({
		title : '选择专业'
	});
	$("#kc").selectpicker({
		title : '选择课程'
	});
	
	$("#kkxq").selectpicker({
		title : '选择开课学期'
	});
	
	
	$("#khfs").selectpicker({
		title : '选择考核方式'
	});


	function selectYear(year,collegeVal,profesionVal) {
		$.post('curriculum/getCollege', {
			year : year
		}, function(result) {
				$('#college,#profesion').html(
						'<option style="display: none"></option>');

				for (var i = 0; i < result.length; i++) {
					$('#college').append(
							"<option value='"+result[i]+"'>" + result[i]
									+ "</option>");
				}
				$('#college').selectpicker('refresh');
				$('#profesion').selectpicker('refresh');
				if(collegeVal!=null&&collegeVal!=undefined){
					$('#college').selectpicker('val',collegeVal);
					selectCollege(collegeVal,profesionVal);
					
				}
		})

	}

	function selectCollege(college,profesionVal) {
		var year = $('#year').val();
		$.post('curriculum/getProfesion', {
			college : college,
			year : year
		},
				function(result) {
						$('#profesion').html(
								'<option style="display: none"></option>');
						for (var i = 0; i < result.length; i++) {
							$('#profesion').append(
									"<option value='"+result[i]+"'>"
											+ result[i] + "</option>");
						}
						$('#profesion').selectpicker('refresh');
						if(profesionVal!=null&&profesionVal!=undefined){
							$('#profesion').selectpicker('val',profesionVal);
						}
				})

	}
	var options = {
		excluded : [':disabled'],
		message : '验证不通过',
		feedbackIcons : {
			valid : 'glyphicon glyphicon-ok',
			invalid : 'glyphicon glyphicon-remove',
			validating : 'glyphicon glyphicon-refresh'
		},
		fields : {
			
			year : {
				validators : {
					notEmpty : {
						message : '年级不能为空'
					}
				}
			},
			college : {
				validators : {
					notEmpty : {
						message : '学院不能为空'
					}
				}
			},
			profesion : {
				validators : {
					notEmpty : {
						message : '专业不能为空'
					}
				}
			},
			kcmc : {
				validators : {
					notEmpty : {
						message : '课程名称不能为空'
					}
				}
			},
			kcdm : {
				validators : {
					notEmpty : {
						message : '专业不能为空'
					},
					regexp: {
                        regexp: /^[0-9]*$/,
                        message: '所输入的字符不符要求'
                    }
				}
			},
			kclb : {
				validators : {
					notEmpty : {
						message : '课程类别不能为空'
					},
					 callback: {
	                     message: '请选择两项课程类别',
	                     callback: function (value, validator) {

	                         if (value == -1 || value.length<2) { //-1是--请选择--选项
	                             return false;
	                         } else {
	                             return true;
	                         }

	                     }
				}
				}
		},
		zongxs : {
			validators : {
				regexp: {
                    regexp: /^[1-9]\d*$/,
                    message: '所输入学时不符要求'
                }
			}
		},
		jsxs : {
			validators : {
				regexp: {
                    regexp: /^[1-9]\d*$/,
                    message: '所输入讲授学时不符要求'
                }
			}
		},
		syxs : {
			validators : {
				regexp: {
                    regexp: /^[1-9]\d*$/,
                    message: '所输入实验学时不符要求'
                }
			}
		},
		sex :{
			validators : {
				notEmpty : {
					message : '课程名称不能为空'
				}
			}
		},
		sjxs : {
			validators : {
				regexp: {
                    regexp: /^[1-9]\d*$/,
                    message: '所输入上机学时不符要求'
                }
			}
		},
		qtxs : {
			validators : {
				regexp: {
                    regexp: /^[1-9]\d*$/,
                    message: '所输入其他学时不符要求'
                }
			}
		},
		xf : {
			validators : {
				regexp: {
                    regexp: /^\d+(\.\d+)?$/,
                    message: '所输入学分不符要求'
                }
			}
		},
		zhouxs : {
			validators : {
				regexp: {
                    regexp: /^[1-9]\d*$/,
                    message: '所输入每周学时不符要求'
                }
			}
		},
		kkxq : {
			validators : {
				notEmpty : {
					message : '开课学期不能为空'
				}
			}
		}
		
	}
};
	$('#formEdit').bootstrapValidator(options).on('success.form.bv',
			function(e) {
				e.preventDefault(); //很重要没有它则会提交默认表单，不会做下面的ajax表单提交
				var xf=$('#xf').val();
				if(xf==null||xf==''){
					$('#xf').val(0);
				}
				var fd = new FormData($("#formEdit")[0]);
				$.ajax({
					url : "curriculum/save",
					type : "POST",
					data : fd,
					processData : false, // 不处理数据  
					contentType : false, // 不设置内容类型
					success : function(result) {
						if (result.success) {
							$('#myModal').modal("hide");
						} else {
							bootbox.alert(result.msg);
						}
					},
					error : function(result) {
						alert(result);
					}
				});
			});
</script>
<script type="text/javascript">
$(function (){
	
	$.post('curriculum/getKc',function (result){
		if(result.length<1){
			bootbox.alert("还没有数据，请前往数据字典添加");
	    	return;
		}
	    for(var i=0;i<result.length;i++){
	    	 $('#kc').append("<option value='"+result[i].id+"'>" + result[i].kcmc
	 		+ "</option>");
	    }
	    $('#kc').selectpicker('refresh');
	})
	
});

if([[${model!=null}]]){
	var year = '[[${model==null?null:model.profession.year}]]';
	var college = '[[${model==null?null:model.profession.college}]]';
	var profesion = '[[${model==null?null:model.profession.profesion}]]';
	var kkxq='[[${model==null?null:model.kkxq}]]';
	var khfs='[[${model==null?null:model.khfs}]]';
	$('#year').selectpicker('val', year);
	selectYear(year,college,profesion);
	var list = '[[${model==null?null:model.lbs}]]'.replace(' ', '')
			.slice(1, -1).split(',');
	$('#lbSelect').selectpicker('val', list);
	$('#kkxq').selectpicker('val', kkxq);
	$('#khfs').selectpicker('val', khfs);
	
}else{
	var year = $('#search_year').val();
	var college = $('#search_college').val();
	var profesion = $('#search_profesion').val();
	var kkxq = $('#search_kkxq').val();
	$('#year').selectpicker('val', year);
	$('#kkxq').selectpicker('val', kkxq);
	selectYear(year,college,profesion);
}

function setKc() {
	var selectKcid=$('#kc').val();
	$.post('curriculum/getKc',function (result){
		for(var i=0;i<result.length;i++){
			if(result[i].id==selectKcid){
				$('input[name="kcmc"]').val(result[i].kcmc);
				$('input[name="kcdm"]').val(result[i].kcdm);
				$('input[name="zongxs"]').val(result[i].zongxs);
			    $('input[name="zhouxs"]').val(result[i].zhouxs);
			    $('input[name="xf"]').val(result[i].xf);
			    $('input[name="jsxs"]').val(result[i].jsxs);
			    $('input[name="syxs"]').val(result[i].syxs);
			    $('input[name="sjxs"]').val(result[i].sjxs);
			    $('input[name="qtxs"]').val(result[i].qtxs);
			}
		}
	})
};
</script>